package com.sd365.permission.centre.service.job;/*
 * @Auther:hantianbing
 * @Date:2022/12/21
 * @Description:
 */

import com.sd365.permission.centre.dao.mapper.RoleResourceMapper;
import com.sd365.permission.centre.entity.RoleIdAndResourceApis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName StoreRoleResourceToRedis
 * @Description
 * @Author hantianbing
 * @Date 2022/12/21 10:20
 **/
@Component
public class StoreRoleResourceToRedis {

    /**
     * 控制roleResource表
     */
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 控制操作redis
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0/20 * * * * ?")
    public void storeRoleResourceToRedis(){
        //获取角色资源
        List<RoleIdAndResourceApis> roleIdAndResourceApis = roleResourceMapper.selectAllRoleResourcesApi();
        //存入redis
        for (RoleIdAndResourceApis resource : roleIdAndResourceApis) {
            redisTemplate.opsForValue().set("roleApis:"+resource.getRoleId().toString(), resource.getResourceApis());
        }
    }
}
